<?php
/**
 * 
 * @author nhan
 *
 */
class Dao_ProductDAO extends Dao_AbstractDAO {
	
	public function __construct($db = null) {
		parent::__construct($db);
		$this->tableName = "M_PRODUCT";
		$this->key = "ID";
		$this->classModel = "Model_Product";
	}
	
	/**
	 * get newest product
	 * @param string $number
	 * @return multitype:unknown
	 */
	public function getNewestProduct($number = null) {
		
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE ";
		$query .= " T.ENABLE = '1' ";
		$query .= " AND T.DELETE_TM IS NULL ";
		$query .= " ORDER BY T.CREATE_TM DESC ";
		
		if ($number !== null) {
			$query .= " limit 0, $number ";
		}
		$db = $this->db;
		$res = $db->query($query);
		
		return $this->getObjects($res);
	}
	
	/**
	 * get top product
	 * @param string $number
	 * @return multitype:unknown
	 */
	public function getTopProduct($number = null) {
		
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE ";
		$query .= " T.ENABLE = '1' ";
		$query .= " AND T.DELETE_TM IS NULL ";
		$query .= " AND T.TOP = '1' ";
		$query .= " ORDER BY T.CREATE_TM DESC ";
		
		if ($number !== null) {
			$query .= " limit 0, $number ";
		}
		
		$db = $this->db;
		$res = $db->query($query);
		
		return $this->getObjects($res);
	}
	
	
	/**
	 * get bestseller product
	 * @param string $number
	 * @return multitype:unknown
	 */
	public function getBestSellingProduct($number = null) {
		
		$query = " SELECT * FROM $this->tableName T ";
		$query .= " WHERE ";
		$query .= " T.ENABLE = '1' ";
		$query .= " AND T.DELETE_TM IS NULL ";
		$query .= " ORDER BY T.SOLD_TIMES DESC ";
		
		if ($number !== null) {
			$query .= " limit 0, $number ";
		}
		
		$db = $this->db;
		$res = $db->query($query);
		
		return $this->getObjects($res);
	}
	
	/**
	 * Lay danh sach san pham theo id loai san pham
	 * @param string $idCategory
	 * @param int $number
	 * @return array Model_Product
	 */
	public function getProductByCategoryId ($idCategory, $number = null) {
	
		$query = " 
		SELECT
			M_PRODUCT.ID,
			M_PRODUCT.ID_CATEGORY,
			M_PRODUCT.`NAME`,
			M_PRODUCT.PRICE,
			M_PRODUCT.VIEW_TIMES,
			M_PRODUCT.LIKE_TIMES,
			M_PRODUCT.SOLD_TIMES,
			M_PRODUCT.IMAGE_FILE,
			M_PRODUCT.LINK_REFER,
			M_PRODUCT.TOP,
			M_PRODUCT.`ENABLE`,
			M_PRODUCT.CREATE_TM,
			M_PRODUCT.CREATE_USR_ID,
			M_PRODUCT.UPDATE_TM,
			M_PRODUCT.UPDATE_USR_ID,
			M_PRODUCT.DELETE_TM
		FROM
			M_PRODUCT
			INNER JOIN M_CATEGORY 
			ON M_PRODUCT.ID_CATEGORY = M_CATEGORY.ID
		
		WHERE
			M_PRODUCT.ID_CATEGORY = '".$idCategory."'
			AND 
			M_PRODUCT.DELETE_TM IS  NULL
		AND
			M_PRODUCT.`ENABLE` = '1'
		";
		
		
		

	
		if ($number !== null) {
			$query .= " limit 0, $number ";
		}
	
		$db = $this->db;
		$res = $db->query($query);
	
		return $this->getObjects($res);
	}
	
}